package com.fyyg.common.wechat;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;

public class WechatLoginUtil {
	
	/**
	 * 匹配登陆模式
	 * 微信登录授权地址   snsapi_userinfo模式需手动授权
	 * @param redirect_uri	:授权后重定向地址， 此参数需要：urlencode
	 * @param state			:附带参数,可自定义
	 * @param type			：snsapi_base  snsapi_userinfo			
	 * :snsapi_base  	是用来获取进入页面的用户的openid 静默授权并自动跳转到回调页
	 * :snsapi_userinfo 是用来获取用户的基本信息的  授权需要用户手动同意 在授权后获取该用户的基本信息。 
	 * @return
	 */
	public static String getWeiXinLoginRul(String redirect_uri,String state,String type){
		String url = WeChatObject.SNSAPI_USERINFO_URL;
		try {
			String ENCODING="utf-8";
			String strURL= URLEncoder.encode(redirect_uri, ENCODING);
			url+="?appid="+WeChatObject.MCH_APPID;
			url+="&redirect_uri="+strURL;
			url+="&response_type=code";
			url+="&scope="+type;
			url+="&state="+state+"#wechat_redirect";
		} catch (Exception e) {
			e.printStackTrace();
		}
		return url;
	}
	
	
	
	/**
	 * 通过code换取网页授权access_token
	 * @param code		:第一步获取的code参数 
	 * @return 
	 */
	public static  String getWeiXinAccessTokenUrl(String code){
		String url = WeChatObject.ACCESS_TOKEN_URL;
		try {
			url+="?appid="+WeChatObject.MCH_APPID;
			url+="&secret="+WeChatObject.MCH_APPSECRET;
			url+="&code="+code;
			url+="&grant_type=authorization_code";
		} catch (Exception e) {
			e.printStackTrace();
		}
		return url;
	}
	
	
	/**
	 * 通过access_token  openid 拉取用户信息
	 * @param access_token	：
	 * @param openid 		：用户的唯一标识
	 * @return 
	 */
	public static  String getWeiXinUserInfoUrl(String access_token,String oppenId){
		String url = WeChatObject.GET_USER_INFO_URL;
		try {
			url+="?access_token="+access_token;
			url+="&openid="+oppenId;
			url+="&lang=zh_CN";
		} catch (Exception e) {
			e.printStackTrace();
		}
		return url;
	}
	
	
	/**
	 * HTTP 请求
	 */
	public static String httpConnection(String URL){
		StringBuffer sb = new StringBuffer("");
		try {
			URL url = new URL(URL);
			HttpURLConnection connection = (HttpURLConnection) url.openConnection();
			connection.setDoOutput(true);
			connection.setDoInput(true);
			connection.setConnectTimeout(5000);// 设置 HttpURLConnection的断开时间
			connection.setRequestMethod("POST");// 设置 HttpURLConnection的请求方式   
			connection.setRequestProperty("Accept-Charset", "UTF-8"); // 设置 HttpURLConnection的字符编码
			connection.setUseCaches(false);
			connection.setInstanceFollowRedirects(true);
			connection.connect();
	        PrintWriter out = new PrintWriter(new OutputStreamWriter(connection.getOutputStream(), "UTF-8"));
		 	out.flush();
		 	out.close();
			InputStream is = connection.getInputStream();
			BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
			String lines;
			while ((lines = reader.readLine()) != null) {
				lines = new String(lines.getBytes(), "utf-8");
				sb.append(lines);
			}
			is.close();
			connection.disconnect();
			
		}catch (Exception e) {
			e.printStackTrace();
		}
		return sb.toString();
	}
	
	
	
}
